﻿package plugin.inviteteam
{
    import com.core.*;
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import flash.display.*;
    import flash.events.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.newteam.model.*;
    import uiresource.newteam.*;

    public class InviteTeamCtrl extends InviteTeamUI
    {
        private var jujArr:Array;
        private var yaoqArr:Array;

        public function InviteTeamCtrl()
        {
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            this.addEvent();
            return;
        }// end function

        public function initShow() : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_1:* = ModelProxy.getValue("team", "inviteteam");
            if (_loc_1 == null)
            {
                _loc_1 = [];
            }
            var _loc_2:Boolean = true;
            this.yaoqArr = [];
            _loc_3 = 0;
            while (_loc_3 < _loc_1.length)
            {
                
                _loc_4 = _loc_1[_loc_3].rid;
                if (this.yaoqArr.length > 0)
                {
                    _loc_5 = 0;
                    while (_loc_5 < this.yaoqArr.length)
                    {
                        
                        if (this.yaoqArr[_loc_5].rid == _loc_4)
                        {
                            break;
                        }
                        _loc_5++;
                    }
                }
                this.yaoqArr.push(_loc_1[_loc_3]);
                _loc_3++;
            }
            list.array = this.yaoqArr;
            return;
        }// end function

        private function addEvent() : void
        {
            closebtn.addEventListener(MouseEvent.CLICK, this.clickClose);
            list.renderHandler = new Handler(this.listHandler);
            return;
        }// end function

        private function mash() : Sprite
        {
            var _loc_1:* = new Sprite();
            _loc_1.graphics.beginFill(16711680, 1);
            _loc_1.graphics.drawCircle(0, 0, 40);
            _loc_1.graphics.endFill();
            return _loc_1;
        }// end function

        private function listHandler(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Label = null;
            var _loc_5:Label = null;
            var _loc_6:Label = null;
            var _loc_7:Label = null;
            var _loc_8:Button = null;
            var _loc_9:Button = null;
            var _loc_10:Image = null;
            if (param2 < list.array.length)
            {
                _loc_3 = list.array[param2];
                _loc_4 = param1.getChildByName("name") as Label;
                _loc_5 = param1.getChildByName("career") as Label;
                _loc_6 = param1.getChildByName("lv") as Label;
                _loc_7 = param1.getChildByName("yaoqian") as Label;
                _loc_8 = param1.getChildByName("tonyi") as Button;
                _loc_9 = param1.getChildByName("juj") as Button;
                _loc_10 = param1.getChildByName("head") as Image;
                _loc_10.url = Config.getUrl("assets/images/head/" + _loc_3.career + "_" + _loc_3.sex + ".png", ResourceType.NORMAL);
                _loc_4.text = _loc_3.name;
                _loc_6.text = _loc_3.level;
                _loc_5.text = this.job(int(_loc_3.career));
                if (_loc_3.duiwu && _loc_3.duiwu == 2)
                {
                    _loc_7.text = "申请入您的队";
                }
                else
                {
                    _loc_7.text = "邀请你组队";
                }
                _loc_8.clickHandler = new Handler(this.tongyihandler, [_loc_3]);
                _loc_9.clickHandler = new Handler(this.jujHandler, [_loc_3.rid]);
            }
            return;
        }// end function

        private function jujHandler(param1:int) : void
        {
            var _loc_2:int = 0;
            if (!this.jujArr)
            {
                this.jujArr = new Array();
            }
            if (this.yaoqArr)
            {
                _loc_2 = 0;
                while (_loc_2 < this.yaoqArr.length)
                {
                    
                    if (this.yaoqArr[_loc_2].rid == param1)
                    {
                        this.yaoqArr.splice(_loc_2, 1);
                    }
                    _loc_2++;
                }
            }
            list.array = this.yaoqArr;
            this.jujArr.push(int(param1));
            PluginManager.instance.sendToServer("team", CMDteam.TEAM_REJECT, {rid:param1});
            if (list.array.length <= 0)
            {
                this.close();
            }
            return;
        }// end function

        private function tongyihandler(param1:Object) : void
        {
            var _loc_2:int = 0;
            if (param1.duiwu == 2)
            {
                PluginManager.instance.sendToServer("team", CMDteam.AGREE_apply, param1.rid);
                if (this.yaoqArr)
                {
                    _loc_2 = 0;
                    while (_loc_2 < this.yaoqArr.length)
                    {
                        
                        if (this.yaoqArr[_loc_2].rid == param1.rid)
                        {
                            this.yaoqArr.splice(_loc_2, 1);
                        }
                        _loc_2++;
                    }
                }
                list.array = this.yaoqArr;
                if (list.array.length <= 0)
                {
                    this.close();
                }
            }
            else
            {
                PluginManager.instance.sendToServer("team", CMDteam.TEAM_AGREE, param1.rid);
                this.close();
            }
            return;
        }// end function

        private function job(param1:int) : String
        {
            var _loc_2:String = "";
            switch(param1)
            {
                case 1:
                {
                    _loc_2 = "战士";
                    break;
                }
                case 2:
                {
                    _loc_2 = "法师";
                    break;
                }
                case 3:
                {
                    _loc_2 = "道士";
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_2;
        }// end function

        protected function clickClose(event:MouseEvent) : void
        {
            this.close();
            return;
        }// end function

        public function close() : void
        {
            var _loc_1:int = 0;
            var _loc_2:Object = null;
            if (this.yaoqArr)
            {
                _loc_1 = 0;
                while (_loc_1 < this.yaoqArr.length)
                {
                    
                    _loc_2 = this.yaoqArr[_loc_1];
                    this.yaoqArr.splice(_loc_1, 1);
                    _loc_2 = null;
                    _loc_1++;
                }
                this.yaoqArr = null;
            }
            ModelProxy.change("team", "inviteteam", this.yaoqArr);
            ModelProxy.change("plugin", "close", "inviteteam");
            return;
        }// end function

    }
}
